package com.ybdz.scanner.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ybdz.scanner.R;
import com.ybdz.scanner.utils.MyApplication;
import com.ybdz.scanner.utils.MyToast;
import com.ybdz.scanner.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主界面
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.account)
    ImageView mAccount;
    @BindView(R.id.rl_deposit)
    RelativeLayout mRlDeposit;
    @BindView(R.id.rl_take)
    RelativeLayout mRlTake;
    @BindView(R.id.rl_query)
    RelativeLayout mRlQuery;


    public static boolean isExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        String name = SPUtils.getString(MainActivity.this, "usename", "deUseName");
        isLogining(name);
    }

    /**
     * 判定登录状态
     *
     * @param name
     */
    private void isLogining(String name) {
        if (name.equals("deUseName") || name == null) {
            SPUtils.saveString(MainActivity.this, "usename", null);
            MyApplication.setIsLogin(false);
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }
    }


    @OnClick({R.id.account, R.id.rl_deposit, R.id.rl_take, R.id.rl_query})
    public void onViewClicked(View view) {
        if (MyApplication.getIsLogin() == true){
            switch (view.getId()) {
                case R.id.account:              //账户入口
                    startActivity(new Intent(MainActivity.this, AccountActivity.class));
                    break;
                case R.id.rl_deposit:           //入库
                    startActivity(new Intent(MainActivity.this, DepositActivity.class));
                    break;
                case R.id.rl_take:              //出库
                    startActivity(new Intent(MainActivity.this, TakeActivity.class));
                    break;
                case R.id.rl_query:              //查询
                    startActivity(new Intent(MainActivity.this, QueryScanActivity.class));
                    break;
            }
        }else {
            startActivity(new Intent(MainActivity.this,LoginActivity.class));
        }

    }

    /**
     * 连按两次退出app
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void exit() {
        if (!isExit) {
            isExit = true;
            MyToast.show(MainActivity.this, "再按一次退出程序");
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };


}
